#!/system/bin/sh
# Project WIPE https://github.com/yc9559/cpufreq-interactive-opt
# Author: yc9559
# Generated at: Sat May  5 12:18:37 2018

# $1:value $2:file path
function set_value() {
	if [ -f $2 ]; then
		chown 0.0 $2
		chmod 0666 $2
		echo $1 > $2
		chmod 0444 $2
	fi
}

# $1:cpu0 $2:timer_rate $3:value
function set_param() {
	echo $3 > /sys/devices/system/cpu/$1/cpufreq/interactive/$2
}

action=$1
setenforce 0

# RunOnce
if [ ! -f /dev/project_wipe_runonce ]; then
	# make sure that sysfs is RW
	mount -o remount,rw sysfs /sys
fi

set_value 1 /sys/devices/system/cpu/cpu4/online
chown 0.0 /sys/devices/system/cpu/cpu0/cpufreq/interactive/*
chown 0.0 /sys/devices/system/cpu/cpu4/cpufreq/interactive/*
chmod 0666 /sys/devices/system/cpu/cpu0/cpufreq/interactive/*
chmod 0666 /sys/devices/system/cpu/cpu4/cpufreq/interactive/*

# RunOnce
if [ ! -f /dev/project_wipe_runonce ]; then
	# set flag
	touch /dev/project_wipe_runonce

	set_value 2-3 /dev/cpuset/background/cpus
	set_value 2-3 /dev/cpuset/system-background/cpus
	set_value 0-2,4-7 /dev/cpuset/foreground/cpus
	set_value 0-2,4-7 /dev/cpuset/top-app/cpus

	set_value 380000 /sys/devices/system/cpu/cpu0/cpufreq/scaling_min_freq
	set_value 680000 /sys/devices/system/cpu/cpu4/cpufreq/scaling_min_freq

	# shared interactive parameters
	set_param cpu0 timer_rate 20000
	set_param cpu4 timer_rate 20000
	set_param cpu0 timer_slack 180000
	set_param cpu4 timer_slack 180000
	set_param cpu0 io_is_busy 0
	set_param cpu4 io_is_busy 0
fi

if [ "$action" = "powersave" ]; then
	set_param cpu0 boost 0
	set_param cpu4 boost 0
	set_param cpu0 above_hispeed_delay "18000 1280000:58000"
	set_param cpu0 hispeed_freq 1180000
	set_param cpu0 go_hispeed_load 98
	set_param cpu0 boostpulse_duration 18000
	set_param cpu0 target_loads "80 480000:52 680000:58 780000:73 880000:54 980000:67 1180000:93 1480000:98"
	set_param cpu0 min_sample_time 18000
	set_param cpu4 above_hispeed_delay "18000 1280000:58000 1380000:18000 1480000:58000 1780000:138000"
	set_param cpu4 hispeed_freq 1080000
	set_param cpu4 go_hispeed_load 97
	set_param cpu4 boostpulse_duration 38000
	set_param cpu4 target_loads "80 880000:66 980000:76 1280000:92 1380000:79 1480000:97"
	set_param cpu4 min_sample_time 18000
fi

if [ "$action" = "balance" ]; then
	set_param cpu0 boost 0
	set_param cpu4 boost 0
	set_param cpu0 above_hispeed_delay "18000 1480000:58000"
	set_param cpu0 hispeed_freq 1180000
	set_param cpu0 go_hispeed_load 94
	set_param cpu0 boostpulse_duration 18000
	set_param cpu0 target_loads "80 480000:52 680000:45 780000:61 880000:54 980000:70 1080000:66 1180000:97 1280000:66 1480000:98"
	set_param cpu0 min_sample_time 18000
	set_param cpu4 above_hispeed_delay "38000 1180000:18000 1380000:58000 1480000:38000 1580000:58000 1780000:138000"
	set_param cpu4 hispeed_freq 1080000
	set_param cpu4 go_hispeed_load 96
	set_param cpu4 boostpulse_duration 38000
	set_param cpu4 target_loads "80 780000:74 880000:62 980000:75 1180000:13 1280000:74 1380000:78 1480000:95"
	set_param cpu4 min_sample_time 18000
fi

if [ "$action" = "performance" ]; then
	set_param cpu0 boost 0
	set_param cpu4 boost 0
	set_param cpu0 above_hispeed_delay "38000 1180000:18000 1280000:58000 1580000:18000"
	set_param cpu0 hispeed_freq 1080000
	set_param cpu0 go_hispeed_load 91
	set_param cpu0 boostpulse_duration 58000
	set_param cpu0 target_loads "80 480000:55 680000:6 780000:51 980000:70 1080000:65 1180000:87 1280000:69 1480000:98"
	set_param cpu0 min_sample_time 38000
	set_param cpu4 above_hispeed_delay "58000 1780000:38000"
	set_param cpu4 hispeed_freq 1480000
	set_param cpu4 go_hispeed_load 97
	set_param cpu4 boostpulse_duration 38000
	set_param cpu4 target_loads "80 780000:60 880000:56 1080000:67 1180000:53 1280000:76 1380000:92 1480000:88 1580000:98"
	set_param cpu4 min_sample_time 18000
fi

if [ "$action" = "fast" ]; then
	set_param cpu0 boost 1
	set_param cpu4 boost 1
	set_param cpu0 above_hispeed_delay "18000"
	set_param cpu4 above_hispeed_delay "18000 1980000:38000"
	set_param cpu0 hispeed_freq 1480000
	set_param cpu4 hispeed_freq 1580000
	set_param cpu0 target_loads "90"
	set_param cpu4 target_loads "90 1980000:95"
fi

chmod 0444 /sys/devices/system/cpu/cpu0/cpufreq/interactive/*
chmod 0444 /sys/devices/system/cpu/cpu4/cpufreq/interactive/*

exit 0